package cn.sdormitory.canteen.service.impl;


import cn.hutool.core.util.StrUtil;
import cn.sdormitory.canteen.dao.FoodSupplierDao;
import cn.sdormitory.canteen.dao.FoodSupplierGoodsDao;
import cn.sdormitory.canteen.service.FoodSupplierService;
import cn.sdormitory.canteen.vo.FoodSupplierGoodsVO;
import cn.sdormitory.canteen.vo.FoodSupplierVO;
import cn.sdormitory.school.entity.SchoolDuty;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("foodSupplierService")
public class FoodSupplierServiceImpl extends ServiceImpl<FoodSupplierDao, FoodSupplierVO> implements FoodSupplierService {

    @Autowired
    private FoodSupplierDao foodSupplierDao;
    @Autowired
    private FoodSupplierGoodsDao foodSupplierGoodsDao;

    @Override
    public IPage<FoodSupplierVO> getPage(Map<String, Object> params) {
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));

        LambdaQueryWrapper<FoodSupplierVO> wrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(params.get("supplier"))) {
            String supplier = String.valueOf(params.get("supplier"));
            wrapper.likeRight(FoodSupplierVO::getSupplier, supplier);
        }
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public int saveOrUpdateFoodSupplierInfo(FoodSupplierVO vo) {
        int num = 0;
        if(StringUtils.isEmpty(vo.getId())){
            vo.setCreatedTime(new Date());
            num = foodSupplierDao.insert(vo);
        }else{
            vo.setModifyTime(new Date());
            num = foodSupplierDao.updateById(vo);
        }
        return num;
    }

    @Override
    public int deleteFoodSupplierById(Long id) {
        //删除商品再删除供应商
        LambdaQueryWrapper<FoodSupplierGoodsVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FoodSupplierGoodsVO::getSupplierId,id);
        foodSupplierGoodsDao.delete(wrapper);
        return foodSupplierDao.deleteById(id);
    }

    @Override
    public FoodSupplierVO getSupplierInfoById(Long id) {
        LambdaQueryWrapper<FoodSupplierVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FoodSupplierVO::getId,id);
        return foodSupplierDao.selectById(id);
    }

    @Override
    public List<FoodSupplierVO> getSupplierInfoList() {
        return foodSupplierDao.getSupplierInfoList();
    }
}
